package com.darly.rnmodule.module;

import android.app.Activity;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.darly.dlcommon.common.dlog.DLog;
import com.darly.dlcommon.framework.ContextController;
import com.darly.dlcommon.framework.SpEnum;
import com.darly.rnmodule.ModuleEnum;
import com.darly.rnmodule.component.store.OnStoreDialogListener;
import com.darly.rnmodule.obs.InitCfg;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;

/**
 * 商城提供给RN。
 * author:zhangyuhui
 * date:2020-01-1311:37
 * description: 初始化RN需要原生参数的方法。
 */
public class StoreMallModule extends ReactContextBaseJavaModule {

    private Callback callback;

    public StoreMallModule(@NonNull ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @NonNull
    @Override
    public String getName() {
        return ModuleEnum.STORE_MALL_MODULE;
    }

    /**
     * 外面是原生、内部是RN。这里是弹窗入口。并提供返回的回调供使用。完成或者自己关闭，都回调到这里。继续执行。
     * @param callback
     */
    @ReactMethod
    public void openDialog(Callback callback) {
        this.callback = callback;
        try {
            OnStoreDialogListener listener = new OnStoreDialogListener() {
                @Override
                public void onConfirm(String isFinish,String msg) {
                    sendBack(isFinish,msg);
                }

                @Override
                public void onCancel(String isFinish) {
                    sendBack(isFinish,null);
                }

                @Override
                public void onError() {

                }
            };
            InitCfg.getInstance().getCls().newInstance().init().openStoreDialog(getCurrentActivity(),listener);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
    private void sendBack(String isFinish,String msg) {
        if (this.callback != null) {
            WritableMap dismiss = Arguments.createMap();
            dismiss.putString("isFinish", isFinish);
            dismiss.putString("msg", msg);
            this.callback.invoke(dismiss);
        }
    }

    /**
     * RN内部进行点击关闭，或者RN内部全部完成进行关闭
     * @param isFinish true/完成关闭。false/异常关闭
     */
    @ReactMethod
    public void dismissDialog(String isFinish,String msg) {
        try {
            InitCfg.getInstance().getCls().newInstance().init().dismissStoreDialog(isFinish,msg);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }


    /**
     * 提供RN调用退出时的方法。
     */
    @ReactMethod
    public void finish() {
        try {
            getCurrentActivity().setResult(Activity.RESULT_OK);
            getCurrentActivity().finish();
        } catch (Exception e) {

        } catch (Error e) {

        }
    }

}
